package com.shouhuobao.bhi.collectpoint;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.ServicePointBean;

/**
 * 服务网点--收藏
 * 
 * @author coffee <br>
 *         2016-10-22上午10:42:09
 */
public class ServicePointFavoriteActivity extends BaseActivity2 {

	// 常用网点的列表
	private ListView mListView;
	private ServicePointAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppHttp.getInstance().servcePointFavoriteList();
	}

	@Override
	protected void findViewById() {
		setContentView(R.layout.service_point_favorite);
		super.findViewById();
		getAppTitle().setCommonTitle("收藏的投递点");
		mListView = (ListView) findViewById(R.id.listview);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (mAdapter.isEmpty()) {
					return;
				}
				ServicePointBean item = (ServicePointBean) parent.getItemAtPosition(position);
				Intent intent = new Intent(getContext(), ServicePointDetailsActivity.class);
				intent.putExtra("item", item);
				startActivity(intent);
			}
		});
		//
		mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				AppHttp.getInstance().servcePointFavoriteList();
			}
		});
	}

	public boolean handleMessage(final Message msg) {
		switch (msg.what) {
		// 获取收藏的服务网点
		case MsgID.service_point_favorite_list:
			onRefreshComplete();
			final AppResult<ServicePointBean> result = JSON.parseList(msg.obj, ServicePointBean.class);
			if (result.isSuccess()) {
				// 获取常用的网点
				getContext().runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mAdapter = new ServicePointAdapter(getContext(), result.getResultList(),mListView);
						mListView.setAdapter(mAdapter);
					}
				});
			} else {
				showToast(result.getMessage());
			}
			return true;
			// 收藏、取消收藏
		case MsgID.service_point_favorite_yes:
		case MsgID.service_point_favorite_no:
			AppHttp.getInstance().servcePointFavoriteList();
			return false;
		default:
			return super.handleMessage(msg);
		}
	}
}
